import { getToken, login, register, loginhzl, login_h5, isNull, handleUrl, changePwd } from "../utils/request"
import { Modal } from 'antd-mobile';
export default {

	namespace: 'login',

	state: {},

	subscriptions: {

	},

	effects: {
		*register({ payload, type, action }, { call, put, select }) {
			let { callback } = payload;
			delete payload.callback;
			let api_url = API_URL;
			let result = yield call(register, payload, api_url);
			callback && callback(result)
		},
		*login({ payload, type, action }, { call, put, select }) {
			const { username, password, remember, go_on, callback } = payload;
			let api_url = API_URL;
			let result = yield call(login, payload, api_url);
			callback && callback(result)
		},
		*loginhzl({ payload, type, action }, { call, put, select }) {
			const { username, password, remember, go_on, callback } = payload;
			delete payload.callback;
			let api_url = API_URL;
			let result = yield call(loginhzl, payload, api_url);
			callback && callback(result)
		},
		*login_h5({ payload, type, action }, { call, put, select }) {
			const { callback } = payload;
			let result = yield call(login_h5, payload);
			callback && callback(result)
		},
		*getToken({ payload, type, action }, { call, put, select }) {
			let { callback } = payload || {};
			let token = localStorage[MI_APP_CODE + "mi_token"];
			if (!token || true) {
				let result = yield call(getToken);
				if (result) {
					let new_token = result.data.token;
					let _webtitle = !isNull(result.data.title) ? result.data.title : "";
					let api_url = !isNull(result.data.api_url) ? result.data.api_url : "";
					api_url = handleUrl(api_url);
					if (api_url.lastIndexOf("/") !== api_url.length - 1) {
						api_url = api_url + "/"
					}
					localStorage[MI_APP_CODE + "theme_color"] = result.data.theme_color;
					localStorage[MI_APP_CODE + "img_root"] = result.data.img_root;
					localStorage[MI_APP_CODE + "default_page"] = result.data.default_page;
					localStorage[MI_APP_CODE + "default_page_title"] = result.data.default_page_title;
					localStorage[MI_APP_CODE + "mi_token"] = new_token || "";
					localStorage[MI_APP_CODE + "copyright"] = !isNull(result.data.copyright) ? result.data.copyright : "";
					localStorage[MI_APP_CODE + "copyright_url"] = !isNull(result.data.copyright_url) ? result.data.copyright_url : "";
					localStorage[MI_APP_CODE + "logo"] = !isNull(result.data.logo) ? result.data.logo : "";
					localStorage[MI_APP_CODE + "website"] = _webtitle;
					localStorage[MI_APP_CODE + "webname"] = !isNull(result.data.name) ? result.data.name : _webtitle;
					localStorage[MI_APP_CODE + "name"] = result.data.name;
					localStorage[MI_APP_CODE + "config"] = !isNull(result.data.config_json) && Object.keys(result.data.config_json).length > 0 ? JSON.stringify(result.data.config_json) : "";
					callback && callback(result)
				}
			}
		},
		*changePwd({ payload }, { call, put, select }) {
			let { callback } = payload;
			const api_url = API_URL;
			let result = yield call(changePwd, payload, api_url);
			if (result) {
				callback && callback(result)
			}
		},
	},

	reducers: {

	}

};
